package com.cly.order.controller;

import com.cly.common.entity.order.dto.OrderDTO;
import com.cly.common.entity.order.po.Order;
import com.cly.common.entity.result.Result;
import com.cly.common.exception.CommonException;
import com.cly.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderDTO
     * @return
     */
    @PostMapping("/create")
    public Result createOrder(@RequestBody OrderDTO orderDTO) {
        return orderService.createOrder(orderDTO);
    }

    /**
     * 获取订单
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }

    /**
     * 修改订单状态
     * @param orderId
     * @param status
     */
    @PutMapping("/status/{orderId}/{status}")
    public void updateOrderStatus(@PathVariable Long orderId,@PathVariable Integer status){
        try {
            orderService.updateOrderStatus(orderId, status);
        } catch (Exception e) {
            throw new CommonException("修改订单状态失败",e);
        }
    }
}
